'''
This file is part of ModeliMark.

ModeliMark is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

ModeliMark is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with ModeliMark.
If not, see <http://www.gnu.org/licenses/>.

Copyright 2011 Jens Frenkel
'''

import basemodelgenerator
import os

class FlatClass(basemodelgenerator.BaseModelGenerator):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
              

    def generateModelSource(self, Number, dir):        

        assert(isinstance(Number,int))
        assert(isinstance(dir,str))

        modelname = 'modelflat_%s'%(Number)
        workdir = '%s/%s'%(dir,modelname)
        filename = '%s/%s.mo'%(workdir,modelname) 

        # new directory
        if not os.access(workdir, os.F_OK):
            os.mkdir(workdir)
        
        f = open( filename, 'w' )
        f.write('model %s\n'%(modelname))
        f.write('  input Real inp;\n')

        for i in xrange(Number):
            f.write('  Real v_%s;\n'%(str(i)))

        f.write('equation\n')
        for i in xrange(Number-1):
            f.write('\tv_%s = %s + v_%s;\n'%(i, i, i+1))

        f.write('\tder(v_%s) = v_0 + inp;\n'%(Number-1))

        f.write('end %s;\n'%(modelname))
        f.close()  
        
        return modelname, [filename], workdir
        
